package us.codecraft.demo.pipeline;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

public class SeleniumDemo implements PageProcessor {


    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(3000)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36");


    public void process(Page page) {
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.meituan.com/meishi/6120675/");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        WebElement webElement = driver.findElement(By.id("listview"));
        String str = webElement.getAttribute("outerHTML");
        System.out.println(str);

        Html html = new Html(str);
        System.out.println("股票代码："+html.xpath("//tbody/tr[2]/td[2]/a/text()").toString());
        System.out.println("名称："+html.xpath("//tbody/tr[2]/td[3]/a/text()").toString());
        System.out.println("最新价："+html.xpath("//tbody/tr[2]/td[5]/span/text()").toString());
        System.out.println("涨跌额："+html.xpath("//tbody/tr[2]/td[6]/span/text()").toString());
        System.out.println("涨跌幅："+html.xpath("//tbody/tr[2]/td[7]/span/text()").toString());
        System.out.println("振幅："+html.xpath("//tbody/tr[2]/td[8]/span/text()").toString());
        System.out.println("成交量："+html.xpath("//tbody/tr[2]/td[9]/span/text()").toString());
        System.out.println("成交额（万）："+html.xpath("//tbody/tr[2]/td[10]/span/text()").toString());
        System.out.println("昨收："+html.xpath("//tbody/tr[2]/td[11]/span/text()").toString());
        System.out.println("今开："+html.xpath("//tbody/tr[2]/td[12]/span/text()").toString());
        System.out.println("最高："+html.xpath("//tbody/tr[2]/td[13]/span/text()").toString());
        System.out.println("最低："+html.xpath("//tbody/tr[2]/td[14]/span/text()").toString());
      /*  String companyCode = html.xpath("//tbody/tr[1]/td/text()").toString();

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = html.xpath("//tbody/tr[3]/td/text()").toString().split("/")[0];

        String stockCode = html.xpath("//tbody/tr[2]/td/text()").toString().split("/")[0];
        String name = html.xpath("//tbody/tr[5]/td/text()").toString().split("/")[0];
        String department = html.xpath("//tbody/tr[14]/td/text()").toString().split("/")[0];
        System.out.println(companyCode);
        System.out.println(stockCode);
        System.out.println(name);
        System.out.println(department);*/
        driver.close();

    }

    public Site getSite() {
        return site;
    }
  
    public static void main(String[] args) {
        Spider.create(new SeleniumDemo())
                .addUrl("http://quote.eastmoney.com/center/list.html#10_0_0_u?sortType=C&sortRule=-1")
                .thread(5)
                .run();
    }
}
